#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:chengyanan
@file: actionMethod.py
@time: 2019/12/22  10:34 下午
"""
import json
import os
import sys
import datetime
path = os.path.dirname(os.getcwd())
sys.path.append(path)
import time
from selenium.webdriver import ChromeOptions
from selenium import webdriver
from base.find_element import FindElement


class ActionMethod:
    def open_brower(self, brower):
        print("open_brower~~~>>>>>", brower)
        if brower == "chrome":
            options = ChromeOptions()
            options.add_argument('--kiosk')
            self.driver = webdriver.Chrome(options=options)  # 放大浏览器

        elif brower == 'firfox':
            self.driver = webdriver.Firefox()
        elif brower == "edge":
            self.driver = webdriver.Edge()

    def get_url(self, url):
        self.driver.get(url)

    def get_element(self, key):
        find_element = FindElement(self.driver)
        element = find_element.get_element(key)
        return element

    def element_send_keys(self, value, key):
        element = self.get_element(key)
        if type(value) == float:
            element.send_keys(int(value))
        else:
            element.send_keys(value)

    def click_element(self, key):
        element = self.get_element(key)
        element.click()

    def sleep_time(self, duration):
        duration = int(duration)
        time.sleep(duration)

    def close_browser(self):
        self.driver.close()

    def get_title(self):
        title = self.driver.title
        print("title", title)
        return title

    def get_element_value(self, key):
        find_element = FindElement(self.driver)
        element = find_element.get_element(key)
        print("element.text-------->", element.text)
        return element.text

    def slide_up(self, value):
        js = 'window.scrollTo(0,1500)'
        self.driver.execute_script(js)

    def get_console(self, value=None):
        for col in self.driver.get_log('browser'):
            if col["level"] == "WARNING":
                base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
                log_dir = os.path.join(base_dir, "log/logs/")
                log_file = datetime.datetime.now().strftime("%Y-%m-%d") + ".json"
                log_name = log_dir + "/" + log_file
                print("WARNING==========>", col["level"])
                with open(log_name,"w") as f:
                    json.dump(col,f)


if __name__ == '__main__':
    action = ActionMethod()
    action.get_console()
